#include "cmserver.h"
#include"networkmanager.h"
#include"Json/reader.h"
#include"Json/value.h"
#include"logicplayer.h"
#include<iostream>
#include<string.h>

using namespace std;

CMServer::CMServer():
    TCPServer(NULL,NULL,6036,EPOLL_MODE,10)
{

}

CMServer::~CMServer()
{

}

void CMServer::OnRecv(int clientfd, char *msg, int flag)
{
    NetWorkManager::getInstance()->setFd(clientfd);
    if(flag==DISCONN_FLAG)
    {
        Json::Value value;
        LogicPlayer::getInstance()->c2sPlayerLeave(value);
    }
    else
    {
        Json::CharReaderBuilder readerBuilder;
        Json::CharReader* reader = readerBuilder.newCharReader();
        Json::Value value;
        std::string error;
        if (reader->parse(msg, msg + strlen(msg), &value, &error))
        {
            cout<<"recv:"<<value.toStyledString()<<endl;
            NetWorkManager::getInstance()->dispatch(value["MessageId"].asInt(), value);
        }
    }
}

//void CMServer::DoWroldTalkMsg(const int& fd,WorldTalk_Msg* msg)
//{
//    cout<<m_playermaps[fd].rolename<<" in the world say: "<<msg->msg<<endl;
//    msg->fd=fd;
//    for(auto var:m_playermaps)
//    {
//        if(var.first!=fd)
//        {
//            write(var.first,(char*)msg,sizeof(WorldTalk_Msg));
//        }
//    }
//}

//void CMServer::DoPrivateTalkMsg(const int& fd,PrivateTalk_Msg* msg)
//{
//    if(m_playermaps.find(msg->dest)!=m_playermaps.end())
//    {
//        msg->fd=fd;
//        cout<<m_playermaps[fd].rolename<<" say "<<msg->msg<<" to "<<m_playermaps[msg->dest].rolename<<endl;
//        write(msg->dest,(char*)msg,sizeof(PrivateTalk_Msg));
//    }
//}

//void CMServer::DoInitDataMsg(const int& fd,InitData_Msg* rlmsg)
//{
//    m_playermaps[fd].playername=rlmsg->playername;
//    m_playermaps[fd].rolename=rlmsg->rolename;
//    m_playermaps[fd].playertype=rlmsg->playertype;
//    m_playermaps[fd].curmap=rlmsg->curmap;
//    m_playermaps[fd].blood=rlmsg->blood;
//    m_playermaps[fd].mana=rlmsg->mana;
//    m_playermaps[fd].attack=rlmsg->attack;
//    m_playermaps[fd].defense=rlmsg->defense;
//    m_playermaps[fd].grade=rlmsg->grade;
//    cout<<rlmsg->playername<<":"<<rlmsg->rolename<<" online!"<<endl;
//    for(auto var:m_playermaps)
//    {
//        if(var.first!=fd)
//        {
//           InitData_Msg smsg;
//           smsg.type=M_InitData;
//           strcpy(smsg.playername,m_playermaps[var.first].playername.c_str());
//           strcpy(smsg.rolename,m_playermaps[var.first].rolename.c_str());
//           strcpy(smsg.playertype,m_playermaps[var.first].playertype.c_str());
//           smsg.curmap=m_playermaps[var.first].curmap;
//           smsg.blood=m_playermaps[var.first].blood;
//           smsg.mana=m_playermaps[var.first].mana;
//           smsg.attack=m_playermaps[var.first].attack;
//           smsg.defense=m_playermaps[var.first].defense;
//           smsg.grade=m_playermaps[var.first].grade;
//           smsg.fd=var.first;
//           SendMsg(fd,(char*)&smsg,sizeof(smsg));
//           rlmsg->fd=fd;
//           SendMsg(var.first,(char*)rlmsg,sizeof(InitData_Msg));
//        }
//    }
//}

//void CMServer::DoInitPosMsg(const int& fd,InitPos_Msg* msg)
//{
//    m_playermaps[fd].x=msg->x;
//    m_playermaps[fd].y=msg->y;
//    m_playermaps[fd].curmap=msg->curmap;
//    cout<<m_playermaps[fd].rolename<<" initialize position("<<msg->x<<","<<msg->y<<")"<<endl;
//    for(auto var:m_playermaps)
//    {
//        if(var.first!=fd)
//        {
//            msg->fd=fd;
//            SendMsg(var.first,(char*)msg,sizeof(InitPos_Msg));
//            InitPos_Msg smsg;
//            smsg.fd=var.first;
//            smsg.type=M_InitPos;
//            smsg.x=m_playermaps[var.first].x;
//            smsg.y=m_playermaps[var.first].y;
//            smsg.curmap=m_playermaps[var.first].curmap;
//            SendMsg(fd,(char*)&smsg,sizeof(smsg));
//        }
//    }
//}

//void CMServer::DoPlayerLeaveMsg(const int& fd)
//{
//    cout<<m_playermaps[fd].rolename<<" off line"<<endl;
//    for(auto var:m_playermaps)
//    {
//        if(var.first!=fd)
//        {
//            PlayerLeave_Msg msg;
//            msg.type=M_PlayerLeave;
//            msg.fd=fd;
//            SendMsg(var.first,(char*)&msg,sizeof(msg));
//        }
//    }
//    m_playermaps.erase(fd);
//}

// void CMServer::DoPlayerMoveToMsg(const int& fd,MoveTo_Msg* msg)
// {
//     cout<<m_playermaps[fd].rolename<<" move to position("<<msg->x<<","<<msg->y<<")"<<endl;
//     for(auto var:m_playermaps)
//     {
//         if(var.first!=fd)
//         {
//             msg->fd=fd;
//             SendMsg(var.first,(char*)msg,sizeof(MoveTo_Msg));
//         }
//     }
// }

// void CMServer::DoPlayerVerifyPosMsg(const int& fd,VerifyPos_Msg* msg)
// {
//     cout<<m_playermaps[fd].rolename<<" verify position("<<msg->x<<","<<msg->y<<")"<<endl;
//     for(auto var:m_playermaps)
//     {
//         if(var.first!=fd)
//         {
//             msg->fd=fd;
//             SendMsg(var.first,(char*)msg,sizeof(VerifyPos_Msg));
//         }
//         else
//         {
//             m_playermaps[var.first].x=msg->x;
//             m_playermaps[var.first].y=msg->y;
//         }
//     }
// }

// void CMServer::DoUpdateDataMsg(const int& fd,UpdateData_Msg* msg)
// {
//     m_playermaps[fd].attack=msg->attack;
//     m_playermaps[fd].blood=msg->blood;
//     m_playermaps[fd].defense=msg->defense;
//     m_playermaps[fd].grade=msg->grade;
//     m_playermaps[fd].mana=msg->mana;
//     msg->fd=fd;
//     cout<<m_playermaps[fd].rolename<<" update data"<<endl;
//     for(auto var:m_playermaps)
//     {
//         if(var.first!=fd)
//         {
//             SendMsg(var.first,(char*)msg,sizeof(UpdateData_Msg));
//         }
//     }
// }

// void CMServer::doTeamApplyMsg(const int& fd,TeamApply_Msg* msg)
// {
//     msg->fd=fd;
//     if(m_playermaps.find(msg->dest)!=m_playermaps.end())
//     {
//         cout<<m_playermaps[fd].rolename<<" apply to make a team with "<<m_playermaps[msg->dest].rolename<<endl;
//         SendMsg(msg->dest,(char*)msg,sizeof(TeamApply_Msg));
//     }
// }

// void CMServer::DoRefuseTeamMsg(const int& fd,RefuseTeam_Msg* msg)
// {
//     msg->fd=fd;
//     if(m_playermaps.find(msg->dest)!=m_playermaps.end())
//     {
//         cout<<m_playermaps[fd].rolename<<" refused "<<m_playermaps[msg->dest].rolename<<" team apply"<<endl;
//         SendMsg(msg->dest,(char*)msg,sizeof(RefuseTeam_Msg));
//     }
// }


// void CMServer::DoAgreeTeamMsg(const int& fd,AgreeTeam_Msg* msg)
// {
//     msg->fd=fd;
//     if(m_playermaps.find(msg->dest)!=m_playermaps.end())
//     {
//         cout<<m_playermaps[fd].rolename<<" agree "<<m_playermaps[msg->dest].rolename<<" team apply"<<endl;
//         SendMsg(msg->dest,(char*)msg,sizeof(AgreeTeam_Msg));
//     }
// }

// void CMServer::DoTeamMoveMsg(const int& fd,TeamMove_Msg* msg)
// {
//     msg->fd=fd;
//     if(m_playermaps.find(msg->dest)!=m_playermaps.end())
//     {
//         cout<<m_playermaps[fd].rolename<<" team header move "<<m_playermaps[msg->dest].rolename<<" to ("<<msg->x<<","<<msg->y<<")"<<endl;
//         SendMsg(msg->dest,(char*)msg,sizeof(TeamMove_Msg));
//     }
// }

// void CMServer::DoTeamGotoMapMsg(const int& fd,TeamGotoMap_Msg* msg)
// {
//     if(m_playermaps.find(msg->dest)!=m_playermaps.end())
//     {
//         cout<<m_playermaps[fd].rolename<<" team header move "<<m_playermaps[msg->dest].rolename<<" to "<<msg->map<<endl;
//         SendMsg(msg->dest,(char*)msg,sizeof(TeamGotoMap_Msg));
//     }
// }

// void CMServer::DoDissolveTeamMsg(const int& fd,TeamManage_Msg* msg)
// {
//     msg->fd=fd;
//     if(m_playermaps.find(msg->dest)!=m_playermaps.end())
//     {
//         cout<<"Dissolve Team "<<m_playermaps[fd].rolename<<" and "<<m_playermaps[msg->dest].rolename<<endl;
//         SendMsg(msg->dest,(char*)msg,sizeof(TeamManage_Msg));
//     }
// }

// void CMServer::DoTeamFightMsg(const int& fd,TeamFight_Msg* msg)
// {
//     msg->fd=fd;
//     if(m_playermaps.find(msg->dest)!=m_playermaps.end())
//     {
//         cout<<m_playermaps[fd].rolename<<" leader "<<m_playermaps[msg->dest].rolename<<" entry fight"<<endl;
//         SendMsg(msg->dest,(char*)msg,sizeof(TeamFight_Msg));
//     }
// }

// void CMServer::DoPlayerAtkMsg(const int& fd,PlayerAtk_Msg* msg)
// {
//     msg->fd=fd;
//     if(m_playermaps.find(msg->dest)!=m_playermaps.end())
//     {
//         cout<<m_playermaps[fd].rolename<<" attack monster"<<endl;
//         SendMsg(msg->dest,(char*)msg,sizeof(PlayerAtk_Msg));
//     }
// }

// void CMServer::DoMonsterAtkMsg(const int& fd,MonsterAtk_Msg* msg)
// {
//     msg->fd=fd;
//     if(m_playermaps.find(msg->dest)!=m_playermaps.end())
//     {
//         cout<<"monster attack "<<m_playermaps[msg->dest].rolename<<endl;
//         SendMsg(msg->dest,(char*)msg,sizeof(MonsterAtk_Msg));
//     }
// }

// void CMServer::DoPlayerRunMsg(const int& fd,PlayerRun_Msg* msg)
// {
//     msg->fd=fd;
//     if(m_playermaps.find(msg->dest)!=m_playermaps.end())
//     {
//         cout<<m_playermaps[fd].rolename<<" run away"<<endl;
//         SendMsg(msg->dest,(char*)msg,sizeof(PlayerRun_Msg));
//     }
// }

// void CMServer::DoUseMedicationMsg(const int& fd,UseMedication_Msg* msg)
// {
//     msg->fd=fd;
//     if(m_playermaps.find(msg->dest)!=m_playermaps.end())
//     {
//         cout<<m_playermaps[fd].rolename<<" use medication!"<<endl;
//         SendMsg(msg->dest,(char*)msg,sizeof(UseMedication_Msg));
//     }
// }

// void CMServer::DoPlayerDieMsg(const int& fd,PlayerDie_Msg* msg)
// {
//     msg->fd=fd;
//     if(m_playermaps.find(msg->dest)!=m_playermaps.end())
//     {
//         cout<<m_playermaps[fd].rolename<<" die!"<<endl;
//         SendMsg(msg->dest,(char*)msg,sizeof(PlayerDie_Msg));
//     }
// }
